package newsSystem.user;

import newsSystem.dao.UserInfoJDBC;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;

import static newsSystem.dao.UserInfoJDBC.getUserInfoFromDatabase;


public class UserPageThreeJPane {
    private static JTextField userIDField, deptIDField, userPWDField, userNameField, userSexField, idCardField, phoneField, addressField;
    private static JButton submitButton, resetButton;
    public static JPanel createPage(String UserID) {
        JPanel mainPanel = new JPanel(new GridBagLayout());
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.insets = new Insets(5, 5, 5, 1); // 设置组件之间的间距
        gbc.anchor = GridBagConstraints.WEST; // 组件靠左对齐
        // 设置字体大小
        Font fieldFont = new Font("Arial", Font.PLAIN, 18); // 设置文本框字体
        // UserID
        gbc.gridx = 0;
        gbc.gridy = 0;
        mainPanel.add(new JLabel("UserID:"), gbc);

        gbc.gridx = 1;
        gbc.gridy = 0;
        userIDField = new JTextField(15);
//        userIDField.setFont(fieldFont);
        userIDField.setFont(new Font(Font.SERIF, Font.PLAIN, 12)); // 使用系统默认的衬线字体，通常支持中文
        userIDField.setPreferredSize(new Dimension(150, 27)); // 设置文本框的宽度和高度
        mainPanel.add(userIDField, gbc);


        // UserPWD
        gbc.gridx = 0;
        gbc.gridy = 1;
        mainPanel.add(new JLabel("UserPWD:"), gbc);

        gbc.gridx = 1;
        gbc.gridy = 1;
        userPWDField = new JTextField(15);
//        userPWDField.setFont(fieldFont);
        userPWDField.setFont(new Font(Font.SERIF, Font.PLAIN, 12));
        userPWDField.setPreferredSize(new Dimension(150, 27)); // 设置文本框的宽度和高度
        mainPanel.add(userPWDField, gbc);

        // UserName
        gbc.gridx = 0;
        gbc.gridy = 2;
        mainPanel.add(new JLabel("UserName:"), gbc);

        gbc.gridx = 1;
        gbc.gridy = 2;
        userNameField = new JTextField(15);
//        userNameField.setFont(fieldFont);
        userNameField.setFont(new Font(Font.SERIF, Font.PLAIN, 12));
        userNameField.setPreferredSize(new Dimension(150, 27)); // 设置文本框的宽度和高度
        mainPanel.add(userNameField, gbc);

        // UserSex
        gbc.gridx = 0;
        gbc.gridy = 3;
        mainPanel.add(new JLabel("UserSex:"), gbc);

        gbc.gridx = 1;
        gbc.gridy = 3;
        // 创建性别下拉框，并添加选项
        String[] genders = {"男", "女"}; // 可以根据需要添加更多选项
        JComboBox<String> userSexComboBox = new JComboBox<>(genders);

// 设置下拉框的字体和尺寸
        userSexComboBox.setPreferredSize(new Dimension(150, 27)); // 设置下拉框的宽度和高度
// 设置下拉框的字体为系统默认字体
        userSexComboBox.setFont(UIManager.getDefaults().getFont("ComboBox.font"));
        mainPanel.add(userSexComboBox, gbc);

        // IDCard
        gbc.gridx = 0;
        gbc.gridy = 4;
        mainPanel.add(new JLabel("IDCard:"), gbc);

        gbc.gridx = 1;
        gbc.gridy = 4;
        idCardField = new JTextField(15);
//        idCardField.setFont(fieldFont);
        idCardField.setFont(new Font(Font.SERIF, Font.PLAIN, 12));
        idCardField.setPreferredSize(new Dimension(150, 27)); // 设置文本框的宽度和高度
        mainPanel.add(idCardField, gbc);

        // Phone
        gbc.gridx = 0;
        gbc.gridy = 5;
        mainPanel.add(new JLabel("Phone:"), gbc);

        gbc.gridx = 1;
        gbc.gridy = 5;
        phoneField = new JTextField(15);
//        phoneField.setFont(fieldFont);
        phoneField.setFont(new Font(Font.SERIF, Font.PLAIN, 12)); // 使用系统默认的衬线字体，通常支持中文
        phoneField.setPreferredSize(new Dimension(150, 27)); // 设置文本框的宽度和高度
        mainPanel.add(phoneField, gbc);

        // Address
        gbc.gridx = 0;
        gbc.gridy = 6;
        mainPanel.add(new JLabel("Address:"), gbc);

        gbc.gridx = 1;
        gbc.gridy = 6;
        addressField = new JTextField(15);
//        addressField.setFont(fieldFont);
        addressField.setFont(new Font(Font.SERIF, Font.PLAIN, 12));
        addressField.setPreferredSize(new Dimension(150, 27)); // 设置文本框的宽度和高度
        mainPanel.add(addressField, gbc);

        // 提交按钮
        gbc.gridx = 0;
        gbc.gridy = 9;
        gbc.gridwidth = 1; // 设置横跨列数为1
        gbc.anchor = GridBagConstraints.CENTER; // 组件居中对齐
        submitButton = new JButton("提交");
        mainPanel.add(submitButton, gbc);

// 重置按钮
        gbc.gridx = 2; // 将gridx的值更新为1，使重置按钮放置在下一列
        gbc.gridy = 9; // 将gridy的值保持为8，保持与提交按钮在同一行
        resetButton = new JButton("修改");



        // 调用getUserInfoFromDatabase方法获取用户信息
        UserInfo userInfo = getUserInfoFromDatabase(UserID);

// 检查是否成功获取到用户信息
        if (userInfo != null) {
            // 输出用户信息
            System.out.println("用户ID: " + userInfo.getUserID());
            System.out.println("用户密码: " + userInfo.getUserPWD());
            System.out.println("用户名: " + userInfo.getUserName());
            System.out.println("性别: " + userInfo.getUserSex());
            System.out.println("身份证号: " + userInfo.getIDCard());
            System.out.println("电话号码: " + userInfo.getPhone());
            System.out.println("地址: " + userInfo.getAddress());
            // 设置文本框的文本内容为用户信息，并将文本框设为不可编辑
            userIDField.setText(userInfo.getUserID());
            userIDField.setEditable(false);

            userPWDField.setText(userInfo.getUserPWD());
            userPWDField.setEditable(false);

            userNameField.setText(userInfo.getUserName());
            userNameField.setEditable(false);

            // 设置下拉框的默认选中项为用户信息中的性别
            userSexComboBox.setSelectedItem(userInfo.getUserSex());
            userSexComboBox.setEditable(false);

            // 设置下拉框为不可编辑
            idCardField.setText(userInfo.getIDCard());
            idCardField.setEditable(false);

            phoneField.setText(userInfo.getPhone());
            phoneField.setEditable(false);

            addressField.setText(userInfo.getAddress());
            addressField.setEditable(false);
        } else {
            // 输出未找到用户的提示信息
            System.out.println("未找到用户 " + UserID);
        }

        resetButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                // 设置文本框和下拉框为可编辑
                userIDField.setEditable(true);
                userPWDField.setEditable(true);
                userNameField.setEditable(true);
                userSexComboBox.setEditable(true);
                idCardField.setEditable(true);
                phoneField.setEditable(true);
                addressField.setEditable(true);
            }
        });
        submitButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                // 获取当前文本框和下拉框中的信息
                userInfo.setUserID(userIDField.getText());
                userInfo.setUserPWD(userPWDField.getText());
                userInfo.setUserName(userNameField.getText());
                userInfo.setUserSex((String) userSexComboBox.getSelectedItem());
                userInfo.setIDCard(idCardField.getText());
                userInfo.setPhone(phoneField.getText());
                userInfo.setAddress(addressField.getText());

                // 这里是数据库操作部分，假设有个 updateUserInfo 方法负责更新数据库
                // 具体实现根据您的数据库连接和操作方式来写
                boolean isSuccess = false;
                try {
                    isSuccess = updateUserInDatabase(userInfo);
                } catch (SQLException ex) {
                    throw new RuntimeException(ex);
                }

                if (isSuccess) {
                    JOptionPane.showMessageDialog(null, "用户信息修改成功！");
                    // 可以在这里再次设置文本框为不可编辑，根据需求
                    resetEditableStatus();
                } else {
                    JOptionPane.showMessageDialog(null, "用户信息修改失败，请检查输入或联系管理员！");
                }
            }

            private void resetEditableStatus() {
                userIDField.setEditable(false);
                userPWDField.setEditable(false);
                userNameField.setEditable(false);
                userSexComboBox.setEditable(false);
                idCardField.setEditable(false);
                phoneField.setEditable(false);
                addressField.setEditable(false);
            }

            // 假设的数据库更新方法
            private boolean updateUserInDatabase(UserInfo updatedUserInfo) throws SQLException {
                // 实现你的数据库更新逻辑
                UserInfoJDBC.updateUserInDatabase(updatedUserInfo);
                return true; // 返回是否更新成功
            }
        });
        mainPanel.add(resetButton, gbc);
        return mainPanel;
    }

//    public static void main(String[] args) {
//        createPage();
//    }
    }
